package com.auto.survey.activity.config.dao

import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.Query
import androidx.room.RewriteQueriesToDropUnusedColumns
import androidx.room.Transaction
import androidx.room.Update
import com.auto.survey.activity.config.entity.ConfigBean
import com.auto.survey.activity.config.entity.ConfigWithPoints
import com.auto.survey.activity.config.entity.PointConfig
import kotlinx.coroutines.flow.Flow

@Dao
interface ConfigDao {

    // 插入配置并返回配置 ID
    @Insert
    suspend fun insertConfig(config: ConfigBean): Long

    @Transaction
    @Query("SELECT * FROM config_table WHERE id = :id")
    suspend fun getConfigById(id: Int): ConfigBean?
    // 插入点位列表
    @Insert
    suspend fun insertPoints(points: List<PointConfig>)

    // 一次性插入配置及其点位
    @Transaction
    suspend fun insertConfigWithPoints(config: ConfigBean, points: List<PointConfig>):Int {
        val configId = insertConfig(config)

        // 为每个点位设置 position 值
        val pointsWithConfigId = points.mapIndexed { index, point ->
            point.copy(
                configId = configId.toInt(),
                position = index  // 设置位置
            )
        }

        insertPoints(pointsWithConfigId)

        return configId.toInt()
    }

    // 查询所有配置及其点位
    @Transaction
    @Query("SELECT * FROM config_table")
    fun getAllConfigsWithPoints(): Flow<List<ConfigWithPoints>>

    @Transaction
    @Query("""
    SELECT DISTINCT config_table.*
    FROM config_table
    LEFT JOIN point_config_table ON config_table.id = point_config_table.configId
""")
    fun getDistinctConfigs(): Flow<List<ConfigBean>>

    // 获取某个配置的有序点位
    @Transaction
    @Query("""
    SELECT * 
    FROM point_config_table 
    WHERE configId = :configId 
    ORDER BY position ASC
""")
    suspend fun getOrderedPointsForConfig(configId: Int): List<PointConfig>

    @Transaction
    @Query("""
    SELECT 
        config_table.*,
        point_config_table.*
    FROM config_table
    LEFT JOIN point_config_table 
        ON config_table.id = point_config_table.configId
    ORDER BY 
        config_table.id ASC,         -- 先按配置 ID 分组
        point_config_table.position ASC  -- 再按点位位置排序
""")
    @RewriteQueriesToDropUnusedColumns
    fun getConfigsWithOrderedPoints(): Flow<List<ConfigWithPoints>>

    // 删除配置及其关联点位
    @Delete
    suspend fun deleteConfig(config: ConfigBean)

    @Query("DELETE FROM point_config_table")
    suspend fun deleteAllPoint()

    @Query("DELETE FROM config_table")
    suspend fun deleteAllConfig()

    @Update
    suspend fun updateConfig(config: ConfigBean)


    // 根据 configId 删除所有关联点位
    @Query("DELETE FROM point_config_table WHERE configId = :configId")
    suspend fun deletePointsByConfigId(configId: Int)

    // 组合操作：更新配置并替换点位列表
    @Transaction
    suspend fun updateConfigWithPoints(config: ConfigBean, newPoints: List<PointConfig>):Int {
        // 1. 更新 ConfigBean
        updateConfig(config)
        // 2. 删除旧的关联点位
        deletePointsByConfigId(config.id)

        // 3. 插入新的点位列表

        // 为每个点位设置 position 值
        val pointsWithConfigId = newPoints.mapIndexed { index, point ->
            point.copy(
                configId = config.id,
                position = index  // 设置位置
            )
        }

        insertPoints(pointsWithConfigId)
        return config.id
    }
}